package controler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.ConnectionFabric;
import model.Clientes;

/**
 * Servlet implementation class ServletRelClientes
 */
@WebServlet("/ServletRelClientes")
public class ServletRelClientes extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletRelClientes() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
		String sql;
		sql = "select idcliente, nome, telefone, endereco, bairro, estado, cnpj, cpf  from clientes order by nome";
	    
	    try{
	    	
		    Connection con = ConnectionFabric.getConnection();	    	
		    PreparedStatement ps = con.prepareStatement(sql);
		    ResultSet rs = ps.executeQuery();
		    List<Clientes> lista = new ArrayList<Clientes>(); 
		    
		    while(rs.next()){
		    Clientes vend = new Clientes();
		    vend.setIdcliente(rs.getInt("idCliente"));
		    vend.setNome(rs.getString("Nome"));
		    vend.setTelefone(rs.getString("Telefone"));
		    vend.setEndereco(rs.getString("Endereco"));
		    vend.setBairro(rs.getString("Bairro"));
		    vend.setEstado(rs.getString("Estado"));
		    vend.setCnpj(rs.getString("cnpj"));
		    vend.setCPF(rs.getString("Cpf"));
		
		 
		    lista.add(vend);    	
		    }
		    HttpSession sessao = request.getSession();
		    sessao.setAttribute("list", lista);  
		    RequestDispatcher rd = request.getRequestDispatcher("Relatorioclientes.jsp");  
		    rd.forward(request, response); 
		    }catch(Exception ex){	    	
		    	ex.printStackTrace();	
		                        }	
	    
	    
                                      } 
	
                    	
	
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	

                    	
	

	}
}

